package cn.tworice.blog.web;

import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.web.bind.annotation.*;

import java.awt.*;

@Api(tags = "Color颜色代码转换")
@RestController
@RequestMapping("color")
@CrossOrigin
public class ColorController {

    @ApiOperation("通过RGB获取颜色代码")
    @GetMapping("/rgb({rgb})")
    public RequestResult rgb(@ApiParam("rgb") @PathVariable("rgb") String rgb){
        int r,g,b;
        try{
            String[] split = rgb.split(",");
            r = Integer.parseInt(split[0]);
            g = Integer.parseInt(split[1]);
            b = Integer.parseInt(split[2]);
        }catch (Exception ex){
            return new RequestResult(new StatusCode(400, "错误的RGB颜色代码"));
        }

        if(r<0 || r>255 && g<0 || g>255 && b<0 || b>255){
            return new RequestResult(new StatusCode(400, "错误的RGB颜色代码"));
        }
        // 初始化
        RequestResult result = new RequestResult();
        StringBuilder builder = new StringBuilder();
        result.setCode(new StatusCode(200,"请求成功"));
        // Hex
        builder.append("#");
        builder.append(Integer.toHexString(r));
        builder.append(Integer.toHexString(g));
        builder.append(Integer.toHexString(b));
        String hex = builder.toString();
        result.appendData("hex", hex);
        // Int
        int Int = Color.decode(hex).getRGB();
        result.appendData("int",Int);


        result.appendData("rgb","rgb("+rgb+")");

        return result;
    }

    @ApiOperation("通过Hex十六进制获取颜色代码")
    @GetMapping("{hex}")
    public RequestResult hex(@ApiParam("Hex十六进制颜色代码") @PathVariable String hex){
        int i = hex.indexOf("#");
        if(i!=0 && hex.length()!=7){
            return new RequestResult(new StatusCode(400, "错误的Hex十六进制颜色代码"));
        }

        // 初始化
        RequestResult result = new RequestResult();
        result.setCode(new StatusCode(200,"请求成功"));
        StringBuilder builder = new StringBuilder();

        // Int
        int INT = Color.decode(hex).getRGB();
        result.appendData("int",INT);

        // RGB
        Integer r=Integer.parseInt(hex.substring(1, 3),16);
        Integer g=Integer.parseInt(hex.substring(3, 5),16);
        Integer b=Integer.parseInt(hex.substring(5, 7),16);
        builder.append("rgb(");
        builder.append(r);
        builder.append(",");
        builder.append(g);
        builder.append(",");
        builder.append(b);
        builder.append(")");
        result.appendData("rgb",builder.toString());

        result.appendData("hex",hex);

        return result;
    }

    @ApiOperation("通过Int获取颜色代码")
    @GetMapping("-{Int}")
    public RequestResult rgb(@ApiParam("Int颜色代码")@PathVariable Integer Int){
        Int=Int*-1;
        if(Int > -1 || Int < -16777216){
            return new RequestResult(new StatusCode(400, "错误的Int颜色代码"));
        }
        // 初始化
        RequestResult result = new RequestResult();
        result.setCode(new StatusCode(200,"请求成功"));
        StringBuilder builder = new StringBuilder();
        Color color = new Color(Int);

        // RGB
        builder.append("rgb(");
        builder.append(color.getRed());
        builder.append(",");
        builder.append(color.getGreen());
        builder.append(",");
        builder.append(color.getBlue());
        builder.append(")");
        result.appendData("rgb",builder.toString());

        // Hex
        builder.delete(0, builder.length());
        builder.append("#");
        builder.append(Integer.toHexString(color.getRed()));
        builder.append(Integer.toHexString(color.getGreen()));
        builder.append(Integer.toHexString(color.getBlue()));
        result.appendData("hex", builder.toString());

        result.appendData("int", "-"+Int);
        return result;
    }
}
